package com.java.collect.controller;

import com.java.collect.service.CollectService;
import com.java.param.ProductIdRemoveParam;
import com.java.pojo.Collect;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 根据用户id和商品id，保存到收藏中
     * @param collect
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody  Collect collect){
        return collectService.save(collect);

    }

    /**
     * 查看收藏接口
     * @param collect
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody Collect collect){
        return collectService.list(collect.getUserId());
    }
    @PostMapping("/remove")
    public R remove(@RequestBody Collect collect){
        return collectService.remove(collect);
    }

    /**
     * 根据商品id删除全部的收藏记录
     * @param productIdRemoveParam
     * @param result
     * @return
     */
    @PostMapping("/product/remove")
    public R productRemove(@RequestBody @Validated ProductIdRemoveParam productIdRemoveParam,
                           BindingResult result){
        if(result.hasErrors()){
            return R.fail("商品id为空，不可删除！");
        }
        return collectService.productRemove(productIdRemoveParam);
    }

}
